package com.helin.helinhealth.controller;

import com.helin.helinhealth.common.Result;
import com.helin.helinhealth.dto.HealthRecordDTO;
import com.helin.helinhealth.dto.HealthReportDTO;
import com.helin.helinhealth.dto.HealthTrendDTO;
import com.helin.helinhealth.service.HealthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 健康记录控制器
 */
@Slf4j
@Tag(name = "健康监控", description = "健康监控相关接口")
@RestController
@RequestMapping("/api/health")
public class HealthController {

    @Autowired
    private HealthService healthService;

    @Operation(summary = "提交健康数据", description = "记录用户健康数据")
    @PostMapping("/record")
    public Result recordHealth(@RequestBody HealthRecordDTO recordDTO) {
        log.info("提交健康数据: {}", recordDTO);
        Long recordId = healthService.recordHealth(recordDTO);
        return Result.success("健康数据提交成功", recordId);
    }

    @Operation(summary = "获取健康趋势", description = "获取用户健康数据趋势")
    @GetMapping("/trend")
    public Result getHealthTrend(
            @Parameter(description = "记录类型：1-血压 2-血糖") 
            @RequestParam Integer type,
            
            @Parameter(description = "开始日期（格式：yyyy-MM-dd）") 
            @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            
            @Parameter(description = "结束日期（格式：yyyy-MM-dd）") 
            @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        
        log.info("获取健康趋势: 类型={}, 开始日期={}, 结束日期={}", type, startDate, endDate);
        HealthTrendDTO trendDTO = healthService.getHealthTrend(type, startDate, endDate);
        return Result.success("获取健康趋势成功", trendDTO);
    }
    
    @Operation(summary = "获取健康记录列表", description = "获取用户健康记录列表")
    @GetMapping("/records")
    public Result getHealthRecords(
            @Parameter(description = "记录类型：1-血压 2-血糖") 
            @RequestParam(required = false) Integer type,
            
            @Parameter(description = "开始日期（格式：yyyy-MM-dd）") 
            @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            
            @Parameter(description = "结束日期（格式：yyyy-MM-dd）") 
            @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        
        log.info("获取健康记录列表: 类型={}, 开始日期={}, 结束日期={}", type, startDate, endDate);
        List<HealthRecordDTO> records = healthService.getHealthRecords(type, startDate, endDate);
        return Result.success("获取健康记录成功", records);
    }
    
    @Operation(summary = "生成健康报告", description = "根据日期范围生成用户健康报告")
    @GetMapping("/report")
    public Result generateHealthReport(
            @Parameter(description = "开始日期（格式：yyyy-MM-dd）") 
            @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            
            @Parameter(description = "结束日期（格式：yyyy-MM-dd）") 
            @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        
        log.info("生成健康报告: 开始日期={}, 结束日期={}", startDate, endDate);
        HealthReportDTO reportDTO = healthService.generateHealthReport(startDate, endDate);
        
        if (reportDTO == null) {
            return Result.error("指定日期范围内没有健康记录，无法生成报告");
        }
        
        return Result.success("生成健康报告成功", reportDTO);
    }
} 